﻿/*版权声明
 * 
 * 本 河北大学管理学院信息管理系网站 由
 * 河北大学 信管团队 开发
 * 如有任何疑问请联系开发人员
 * 
 * 项目开发者：朱胜贤、孙旭峰、水亚亚、张文元、项玥、吕芳、吴凡、谢静、李亚慧
 * 
 * 如有任何疑问请联系：河北大学信管团队
 * QQ：196396337
 * Email:zagelover@163.com
 * HomePage：http://sxgzs.com/
 * 
 * CopyRight (c) 河北大学信管团队 2010~2020
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Download_Info_Add : System.Web.UI.Page
{
    /// <summary>
    /// 页面加载事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Uid"] == null)
        {
            Response.Redirect("AD_Login.aspx");
            return;
        }
        if (!IsPostBack)
        {
            bind_DownType();
            string ID = Request.QueryString["ID"].ToString();
            if (ID == "")
            {
                lblTitle.Text = "添加下载";
                DateTime dt=DateTime.Now;
                txtUpdateTime.Text = dt.ToString();
            }
            else
            {
                lblTitle.Text = "修改下载";
                Bind_Txt(ID);
                RequiredFieldValidator4.Enabled = false;
                lblDownUrl.Text = "*如果附件不变，请保持为空！";
            }
            
        }
    }
    /// <summary>
    /// Dropdownlist绑定下载类别
    /// </summary>
    protected void bind_DownType()
    {
        string sql = "select * from Download_Type";
        ddlDownType.DataSource = DataAccess.GetDataSet(sql, null).Tables[0];
        ddlDownType.DataTextField = "typeName";
        ddlDownType.DataValueField = "typeName";
        ddlDownType.DataBind();
    }
    /// <summary>
    /// 修改下载绑定数据
    /// </summary>
    /// <param name="ID"></param>
    public void Bind_Txt(string ID)
    {
        //string downUrl = "";
        string sql2 = "select * from Download_Info where dowID=@ID";
        SqlParameter[] para1 = {
                                   new SqlParameter("@ID",SqlDbType.Int)
                               };
        para1[0].Value = ID;
        SqlDataReader DR = DataAccess.GetReader(sql2, para1);
        if (DR.HasRows)
        {
            while (DR.Read())
            {
                ddlDownType.SelectedValue= DR["dowType"].ToString();
                txtTitle.Text = DR["dowTitle"].ToString();
                txtHits.Text = DR["dowHits"].ToString();
                txtKeywords.Text = DR["dowKeywords"].ToString();
                txtDowDescribe.Text = DR["dowdescribe"].ToString();
                ckbHot.Checked = Convert.ToBoolean(DR["dowHot"].ToString());
                ckbIsShow.Checked = Convert.ToBoolean(DR["dowIsShow"].ToString());
                ckbOnTop.Checked = Convert.ToBoolean(DR["dowOntop"].ToString());
                ckbPassed.Checked = Convert.ToBoolean(DR["dowPassed"].ToString());
                lblID.Text = DR["dowID"].ToString();
                hfDownUrl.Value = DR["dowUrl"].ToString(); ;
                txtUpdateTime.Text = DR["dowUpdateTime"].ToString();
            }
        }
    }
    /// <summary>
    /// 响应提交事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        string downloadUrl = "";
        if (this.FileUploadUrl.HasFile)
        {
            string savePath = "~/UploadFiles/files/";
            downloadUrl = FileUploadUrl.FileName;
            string pathToCheck = savePath + downloadUrl;
            string tempfileName = "";
            if (System.IO.File.Exists(Server.MapPath(pathToCheck)))
            {
                int counter = 2;
                while (System.IO.File.Exists(Server.MapPath(pathToCheck)))
                {
                    tempfileName = counter.ToString() + downloadUrl;
                    pathToCheck = savePath + tempfileName;
                    counter++;
                }
                downloadUrl = tempfileName;
            }
            savePath += downloadUrl;
            savePath = Server.MapPath(savePath);
            FileUploadUrl.SaveAs(savePath);
        }
        //判断如果lblID的text值为空，则为添加下载,否则为修改下载
        if (lblID.Text == "")
        {
            string sql1 = "insert into Download_Info(dowTitle,dowKeywords,dowHits,dowHot,dowOntop,dowPassed,dowdescribe,dowType,dowUrl,dowIsShow,dowUpdateTime) values(@dowTitle,@dowKeywords,@dowHits,@dowHot,@dowOntop,@dowPassed,@dowdescribe,@dowType,@dowUrl,@dowIsShow,@dowUpdateTime)";
            SqlParameter[] para = {
            new SqlParameter("@dowTitle",SqlDbType.NVarChar,50),
            new SqlParameter("@dowKeywords",SqlDbType.NVarChar,50),
            new SqlParameter("@dowHits",SqlDbType.Int),
            new SqlParameter("@dowHot",SqlDbType.Bit),
            new SqlParameter("@dowOntop",SqlDbType.Bit),
            new SqlParameter("@dowPassed",SqlDbType.Bit),
            new SqlParameter("@dowdescribe",SqlDbType.NText),
            new SqlParameter("@dowType",SqlDbType.NVarChar,50),
            new SqlParameter("@dowUrl",SqlDbType.NVarChar,50),
            new SqlParameter("@dowIsShow",SqlDbType.Bit),
            new SqlParameter("@dowUpdateTime",SqlDbType.DateTime)
                              };
            para[0].Value = txtTitle.Text;
            para[1].Value = txtKeywords.Text;
            para[2].Value = Convert.ToInt32(txtHits.Text);
            para[3].Value = ckbHot.Checked;
            para[4].Value = ckbOnTop.Checked;
            para[5].Value = ckbPassed.Checked;
            para[6].Value = txtDowDescribe.Text;
            para[7].Value = ddlDownType.SelectedValue;
            para[8].Value = "~/UploadFiles/files/" + downloadUrl;
            para[9].Value = ckbIsShow.Checked;
            para[10].Value = txtUpdateTime.Text;
            if (DataAccess.GetCount(sql1, para) > 0)
            {
                Response.Write("<script>alert('添加下载成功！');location.href='Download_Info_Manage.aspx';</script>");
            }
            else
            {
                lblMessage.Text = "添加下载失败！";
            }
        }
        else
        {
            string sql2 = "update Download_Info set dowTitle=@dowTitle,dowKeywords=@dowKeywords,dowHits=@dowHits,dowHot=@dowHot,dowOntop=@dowOntop,dowPassed=@dowPassed,dowdescribe=@dowdescribe,dowType=@dowType,dowUrl=@dowUrl,dowIsShow=@dowIsShow where dowID=@ID";
            SqlParameter[] para2 = {
            new SqlParameter("@dowTitle",SqlDbType.NVarChar,50),
            new SqlParameter("@dowKeywords",SqlDbType.NVarChar,50),
            new SqlParameter("@dowHits",SqlDbType.Int),
            new SqlParameter("@dowHot",SqlDbType.Bit),
            new SqlParameter("@dowOntop",SqlDbType.Bit),
            new SqlParameter("@dowPassed",SqlDbType.Bit),
            new SqlParameter("@dowdescribe",SqlDbType.NText),
            new SqlParameter("@dowType",SqlDbType.NVarChar,50),
            new SqlParameter("@dowUrl",SqlDbType.NVarChar,50),
            new SqlParameter("@dowIsShow",SqlDbType.Bit),
            new SqlParameter("@ID",SqlDbType.Int)
                              };
            para2[0].Value = txtTitle.Text;
            para2[1].Value = txtKeywords.Text;
            para2[2].Value = Convert.ToInt32(txtHits.Text);
            para2[3].Value = ckbHot.Checked;
            para2[4].Value = ckbOnTop.Checked;
            para2[5].Value = ckbPassed.Checked;
            para2[6].Value = txtDowDescribe.Text;
            para2[7].Value = ddlDownType.SelectedValue;
            if (this.FileUploadUrl.HasFile)
            {
                System.IO.File.Delete(Server.MapPath(hfDownUrl.Value.ToString()));
                para2[8].Value = "~/UploadFiles/files/" + downloadUrl;
            }
            else
            {
                para2[8].Value = hfDownUrl.Value;
            }
            para2[9].Value = ckbIsShow.Checked;
            para2[10].Value = lblID.Text;
            if (DataAccess.GetCount(sql2, para2) > 0)
            {
                Response.Write("<script>alert('修改下载成功！');location.href='Download_Info_Manage.aspx';</script>");
            }
            else
            {
                lblMessage.Text = "修改下载失败！";
            }
        }
    }
    /// <summary>
    /// 响应按钮事件，通过按钮text值不同响应不同的事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (lblTitle.Text=="修改下载")
        {
            btnCancel.Text="取消";
            Response.Redirect("Download_Info_Manage.aspx?downType=");
        }
        else if(lblTitle.Text=="添加下载")
        {
            btnCancel.Text = "清空";
            txtTitle.Text="";
            txtKeywords.Text = "";
            txtHits.Text = "0";
            txtDowDescribe.Text = "";
            ckbHot.Checked = false;
            ckbIsShow.Checked = false;
            ckbOnTop.Checked = false;
            ckbPassed.Checked = false;
        }
    }
}